package com.a0soft.gphone.aTruffleHog.wnd.stat;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfo;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfoKey;
import com.a0soft.gphone.aTruffleHog.di.GoldenInfo;
import com.a0soft.gphone.aTruffleHog.gio.ui.BaseActivity;
import com.a0soft.gphone.aTruffleHog.srvc.HttpConnSrvc;
import com.a0soft.gphone.aTruffleHog.util.aDebug;
import com.a0soft.gphone.aTruffleHog.wnd.BaseListFragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class StatsListFrg extends BaseListFragment {
    public static final int ITEM_OS_STATS = 0; //same number as ITEMS_TITLE
    public static final int ITEM_ROOTED_STATS = 1;
    public static final int ITEM_TOP_DEVICE_STATS = 2;
    public static final int ITEM_TOP_OPERATOR_STATS = 3;
    public static final int ITEM_FIND_BY_CAMERA = 4;
    public static final int ITEM_FIND_BY_CPU = 5;
    
    private int m_nCurSelectedPos;
    
    private String[] ITEMS_TITLE, ITEMS_DESC;
    
    private static final String EXTRA_PREFIX = StatsListFrg.class.getName();
    private static final String EXTRA_CUR_SELECTED_POS = EXTRA_PREFIX + ".csp";
    
    public static final String EXTRA_SEL_ITEM_POS = EXTRA_PREFIX + ".sip";
    public static final String EXTRA_SEL_ITEM_CHANGED = EXTRA_PREFIX + ".sic";
    public static final String EXTRA_SEL_CHANGED_MANUALLY = EXTRA_PREFIX + ".scm";
    
    private static final String LOG_TAG = StatsListFrg.class.getSimpleName();
    
    ////////////////////////////////////////
    @Override public void 
    onCreate(Bundle bdSavedInstanceState)
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onCreate()");
        super.OnCreate(bdSavedInstanceState, true);
        
        ITEMS_TITLE = getResources().getStringArray(R.array.stat_items_title);
        ITEMS_DESC = getResources().getStringArray(R.array.stat_items_desc);
        
        m_nCurSelectedPos = -1;
        
        _ReloadFromArguments(getArguments());
    }
    
    @Override public void 
    onActivityCreated(Bundle bdSavedInstanceState)
    {
        super.onActivityCreated(bdSavedInstanceState);
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onActivityCreated()");
        
        int nLastSelectedPos = m_nCurSelectedPos;
        if (bdSavedInstanceState != null)
            nLastSelectedPos = bdSavedInstanceState.getInt(EXTRA_CUR_SELECTED_POS, m_nCurSelectedPos);
        
        ListView lv = getListView();
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lv.setItemChecked(m_nCurSelectedPos, true);
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "  m_nCurSelectedPos=" + m_nCurSelectedPos);
        
        _SelectionChanged(nLastSelectedPos, false);
        
        _SendDeviceInfo();
    }
    
    @Override public void 
    onSaveInstanceState(Bundle bdOutState)
    {
        super.onSaveInstanceState(bdOutState);
        
        bdOutState.putInt(EXTRA_CUR_SELECTED_POS, m_nCurSelectedPos);
    }

    private void 
    _ReloadFromArguments(Bundle bdArguments) 
    {
        setListAdapter(new MyListAdapter());
    }
    
    @Override public void 
    onListItemClick(ListView lv, View vItem, int nPos, long lID)
    {
        _SelectionChanged(nPos, true);
    }

    private void
    _SelectionChanged(int nNewCheckedPos, boolean bChangedManually)
    {
        int nOldCheckedPos = m_nCurSelectedPos;
        m_nCurSelectedPos = nNewCheckedPos;

        BaseActivity activity = (BaseActivity)getActivity();
        if (activity != null) {
            Intent it = new Intent();
            it.putExtra(EXTRA_SEL_ITEM_POS, nNewCheckedPos);
            it.putExtra(EXTRA_SEL_ITEM_CHANGED, nNewCheckedPos != nOldCheckedPos);
            it.putExtra(EXTRA_SEL_CHANGED_MANUALLY, bChangedManually);
            
            activity.notifyByFragment(this, it);
        }
    }
    
    private void
    _SendDeviceInfo()
    {
        Context ctx = getActivity();
        if (!GoldenInfo.GetInstance(ctx).IsGoldenChanged()) {
            if (aDebug.ENABLED)
                aDebug.d(LOG_TAG, "_SendDeviceInfo(): golden not changed, don't resend device info.");
            return;
        }

        if (aDebug.ENABLED)
            aDebug.d(LOG_TAG, "_SendDeviceInfo(): golden changed, send device info to server.");
        
        DeviceInfo di = DeviceInfo.GetInstance(ctx);
        StringBuilder sb = new StringBuilder(8192);
        di.ToJSONString(sb);
        
        HttpConnSrvc.RequestSendDeviceInformation(ctx, 
                di.GetString(DeviceInfoKey.K.UR_ID),
                sb.toString());
    }
    
    @Override protected void 
    OnDownloadStart(int nCmd) 
    {
        if (nCmd != HttpConnSrvc.CMD_SEND_DEVICE_INFO) return;
        
        if (aDebug.ENABLED)
            aDebug.d(LOG_TAG, "OnDownloadStart()");
        if (getView() == null) return;
    }
    
    @Override protected void 
    OnDownloadFinishedOkay(int nCmd, String strResult) 
    {
        if (nCmd != HttpConnSrvc.CMD_SEND_DEVICE_INFO) return;
        
        if (aDebug.ENABLED)
            aDebug.d(LOG_TAG, "OnDownloadFinishedOkay()");

        if (getView() == null) return;
        
        GoldenInfo.GetInstance(getActivity()).UnsetGoldenChanged();
    }
    
    @Override protected void 
    OnDownloadFinishedError(int nCmd, String strError) 
    {
        if (nCmd != HttpConnSrvc.CMD_SEND_DEVICE_INFO) return;
        
        if (aDebug.ENABLED)
            aDebug.d(LOG_TAG, "OnDownloadFinishedError()");

        if (getView() == null) return;
    }
    
    @Override protected void 
    OnDownloadStop(int nCmd) 
    {
        if (nCmd != HttpConnSrvc.CMD_SEND_DEVICE_INFO) return;
        
        if (aDebug.ENABLED)
            aDebug.d(LOG_TAG, "OnDownloadStop()");

        if (getView() == null) return;
    }
    
    //////////////////////////////
    private class MyListAdapter extends BaseAdapter {
        private LayoutInflater m_lf;
        
        MyListAdapter()
        {
            m_lf = LayoutInflater.from(getActivity());
        }
        
        @Override public int 
        getCount()
        {
            return ITEMS_TITLE.length;
        }

        @Override public String 
        getItem(int nPos)
        {
            return ITEMS_TITLE[nPos];
        }

        @Override public long 
        getItemId(int nPos)
        {
            return nPos;
        }

        @Override public View 
        getView(int nPos, View vConvert, ViewGroup vgParent)
        {
            if (vConvert == null) {
                vConvert = m_lf.inflate(R.layout.stats_list_item, vgParent, false);
            }
            
            TextView tvTitle = (TextView)vConvert.findViewById(R.id.title);
            TextView tvInfo = (TextView)vConvert.findViewById(R.id.info);
            
            tvTitle.setText(ITEMS_TITLE[nPos]);
            tvInfo.setText(ITEMS_DESC[nPos]);
            return vConvert;
        }
    }
}
